package Domain;

import java.io.File;
import java.util.*;

/**
 * @author gth854p
 */
public class ScannerGenerator {
	NFADiagram nfaDiagram;
	LinkedHashMap<String,String> definedClassTokens,regexTokens;
	LinkedHashMap<String,DefinedClass> lex;
	LinkedList<RuleCommand> regexStack;
	RegexParser regexParser;
	public ScannerGenerator()
	{
		
		
	}
	
	/**
	 * UNION is the | character representing the union of two (sub-) regular expressions.
RE_CHAR is the union of two sets:
1)	the ASCII printable characters (see http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters) other than space, \, *, +, ?, |, [, ], (, ), ., ' and "
2)	escaped characters: \ (backslash space), \\, \*, \+, \?, \|, \[, \], \(, \), \., \' and \"
CLS_CHAR is the union of two sets:
1)	the ASCII printable characters (see http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters) other than \, ^, -, [ and ]
2)	escaped characters: \\, \^, \-, \[ and \]

	 */
	//determine if the first char is a bracket. if not, its a regex. if it is, its a defined class/character class
	
	public NFADiagram createScanner(LinkedHashMap<String,String> definedClassTokens,LinkedHashMap<String,String> regexTokens)
	{
		lex = new LinkedHashMap<String,DefinedClass>();
		regexParser = new RegexParser();
		this.definedClassTokens = definedClassTokens;
		this.regexTokens = regexTokens;
		
		
		for(String key : this.definedClassTokens.keySet())
		{
			String value = this.definedClassTokens.get(key);
			//System.out.println(value);
			DefinedClass dc = regexParser.getDefinedClass(value,lex);
			dc.setName(key);
			lex.put(key, dc);
			
		}
		NFADiagram retval = null;
		for(String key : this.regexTokens.keySet())
		{
			String value = this.regexTokens.get(key);
				LinkedList<String> expansion = RegexParser.expandTokens(value, lex);
				//For testing
				System.out.println("REGEX EXPANSION:");
				Iterator<String> iter = expansion.iterator();
				while(iter.hasNext())
					System.out.println(iter.next());
				RuleCommand root = null;
				root = regexParser.orderOps(expansion, lex,root);
				NFADiagram nfa = root.createNFA();
				nfa.getNfaEnd().setTerminal(true);
				if(retval==null)
				{
					retval = nfa;
				}
				else
				{
					NFADiagram union = new NFADiagram();
					
					NFADiagram leftDiagram =retval;
					NFADiagram rightDiagram =nfa;
					NfaState start = new NfaState();
					NfaState end = new NfaState();
					start.addTransition(new StateTransition(start,leftDiagram.getNfaStart(),(char)0));
					start.addTransition(new StateTransition(start,rightDiagram.getNfaStart(),(char)0));
					leftDiagram.getNfaEnd().addTransition(new StateTransition(leftDiagram.getNfaEnd(),end,(char)0));
					rightDiagram.getNfaEnd().addTransition(new StateTransition(rightDiagram.getNfaEnd(),end,(char)0));
					union.setNfaEnd(end);
					union.setNfaStart(start);
					retval = union;
				}
				
		}
		return retval;
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String test = "$DIGIT   [0-9] \n $NON-ZERO    [^0]  IN  $DIGIT \n $LETTER [a-b] \n $IDENTIFIER ($LETTER+|$DIGIT)"; // CHAR   [a-zA-Z] \n UPPER  [^a-z] IN CHAR \n IDENTIFIER     	LETTER (LETTER| DIGIT)* ";



		
	}

}
